<?php
/**
 * Created by PhpStorm.
 * User: cape
 * Date: 2017/11/15
 * Time: 17:17
 */

$mysqli = new mysqli('localhost', 'root', 'root', 'test');
if ($mysqli->connect_errno) {
    echo "Failed to connect to MYSql:(" . $mysqli->connect_errno . ") " . $mysqli->connect_errno;
} else {
    echo '连接成功';
}


echo "<br/>";
$before = microtime();
$session = mysqli_connect('127.0.0.1', 'root', 'root', 'test');
$after = microtime();
echo $after - $before;
echo "<br/>";
$before = microtime();
$session = mysqli_connect('localhost', 'root', 'root', 'test');
$after = microtime();
echo $after - $before;
echo '<hr/>';


//
$t = microtime();
//echo($t . "<br/>");
//echo(date("Y-m-d H:m:s", $t));
//echo '<br/>';
//echo microtime();

echo "<hr/>";


function time_elapsed_A($secs)
{
    $bit = array(
        'y' => $secs / 31556926 % 12,
        'w' => $secs / 604800 % 52,
        'd' => $secs / 86400 % 7,
        'h' => $secs / 3600 % 24,
        'm' => $secs / 60 % 60,
        's' => $secs % 60
    );

    foreach ($bit as $k => $v)
        if ($v > 0) $ret[] = $v . $k;

    return join(' ', $ret);
}


function time_elapsed_B($secs)
{
    $bit = array(
        ' year' => $secs / 31556926 % 12,
        ' week' => $secs / 604800 % 52,
        ' day' => $secs / 86400 % 7,
        ' hour' => $secs / 3600 % 24,
        ' minute' => $secs / 60 % 60,
        ' second' => $secs % 60
    );

    foreach ($bit as $k => $v) {
        if ($v > 1) $ret[] = $v . $k . 's';
        if ($v == 1) $ret[] = $v . $k;
    }
    array_splice($ret, count($ret) - 1, 0, 'and');
    $ret[] = 'ago.';

    return join(' ', $ret);
}


$nowtime = time();
$oldtime = 1335939007;

echo "time_elapsed_A: " . time_elapsed_A($nowtime - $oldtime) . "\n";
echo "time_elapsed_B: " . time_elapsed_B($nowtime - $oldtime) . "\n";


